
#include "app_mqtt.h"
#include <string.h>
#include "MQTTClient.h"
#include <stdio.h>
#include <unistd.h>
/*mqtt客户端句柄*/ 
MQTTClient mqttclient_handle;
MQTTClient_connectOptions conn_opt = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
/*数据到达后外部的回调函数*/
static messageArrive msg_callback;
/*注册数据到达事件的外部回调函数附带的参数*/
static void* param = NULL;
/**
 * @brief 初始化一个mqtt客户端
 * 
 */

/**
 * @brief mqtt客户端断开连接的回调函数
 * 
 * @param context 上下文文本
 * @param cause 导致断开连接的原因
 */
void App_mqtt_connectionLost(void* context, char* cause)
{
    printf("Mqtt Client Disconnect...\n");
}

/**
 * @brief mqtt客户端接收到信息的回调函数
 * 
 * @param context 上下文文本
 * @param topicName 接受到信息的主题
 * @param topicLen 主题长度
 * @param message 接收到的数据
 * @return int 
 */
int App_mqtt_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message)
{
    // MQTTClient_freeMessage(&message);
    printf("mqtt_data[web_to_gateway]->%.*s\n",message->payloadlen,(char*)message->payload);
    
    /*是订阅主题发来的消息*/
    if (strcmp(topicName,MQTT_SUB_TOPIC) == 0)
    {
        if (msg_callback == NULL)
        {
            MQTTClient_freeMessage(&message);
            return 0;
        }
        int res = msg_callback(param,(char*)message->payload,message->payloadlen);
        MQTTClient_freeMessage(&message);
        if (res == -1)
        {
            printf("mqtt message callback run fail\n");
            return 0;
        }
        else if (res == 0)
        {
            return 1;
        }
        
    }
    return -1;
}

/**
 * @brief mqtt客户端发送消息成功触发的回调函数
 * 
 * @param context 上下文文本
 * @param dt 发送的消息的唯一标识可以跟踪该消息的状态
 */
void App_mqtt_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
{
    printf("mqtt publish Success\n");
}
int App_mqtt_Init(void)
{
    int res = 0;
    /*1.创建mqtt客户端*/
    res = MQTTClient_create(&mqttclient_handle,MQTT_CLIENT_SERVER_URI,MQTT_CLIENT_ID,
                      MQTTCLIENT_PERSISTENCE_NONE,NULL);
    if (res != MQTTCLIENT_SUCCESS)
    {
        perror("mqtt client create fail...\n");
        return -1;
    }
    /*2.注册各个事件回调*/
    MQTTClient_setCallbacks(mqttclient_handle,NULL,App_mqtt_connectionLost,App_mqtt_messageArrived,App_mqtt_deliveryComplete);
    /*3.建立服务器连接*/
    res = MQTTClient_connect(mqttclient_handle,&conn_opt);
    if (res == 0)
    {
        printf("mqtt client connect success...\n");
    }
    else
    {
        perror("mqtt client connect fail...\n");
        return -1;
    }
    /*订阅主题接收消息*/
    res = MQTTClient_subscribe(mqttclient_handle,MQTT_SUB_TOPIC,1);
    if (res == 0)
    {
        printf("mqtt client subscribe success...\n");
    }
    else
    {
        perror("mqtt client subscribe fail...\n");
        return -1;
    }
    /*向制定主题发布连接成功消息*/
    pubmsg.payload = "mqtt client connect success!";
    pubmsg.payloadlen = 28;
    pubmsg.qos = 1;
    res = MQTTClient_publishMessage(mqttclient_handle,MQTT_PUB_TOPIC,&pubmsg,NULL);
    if (res != MQTTCLIENT_SUCCESS)
    {
        perror("mqtt client publish fail...\n");
        return -1;
    }
    return 0;
}

/**
 * @brief 撤销已建立的mqtt客户端
 * 
 */
void App_mqtt_Deinit(void)
{
    /*1.断开mqtt客户端的服务器连接*/
    MQTTClient_disconnect(mqttclient_handle,2000);
    /*2.撤销客户端释放内存*/
    MQTTClient_destroy(&mqttclient_handle);
}

void App_mqtt_SendMsg(char *data, int datalen)
{
    pubmsg.payload = data;
    pubmsg.payloadlen = datalen;
    pubmsg.qos = 1;
    int res = MQTTClient_publishMessage(mqttclient_handle,MQTT_PUB_TOPIC,&pubmsg,NULL);
    if (res != MQTTCLIENT_SUCCESS)
    {
        printf("mqtt client publish fail...\n");
    }
}

void App_mqtt_Register_Callback(void* args,messageArrive cb_func)
{
    msg_callback = cb_func;
    param = args; 
}
